#include <GL/glew.h>
#include "sdlglutils.h"

#include "drawObject.h"



DrawObject::DrawObject()
{
    textureFloor = loadTexture("textures/checker02.jpg");
    texture = loadTexture("textures/stainedglass05.jpg");
    textureEarth = loadTexture("textures/EarthMap.jpg");
    //textureGlobe = loadTexture("textures/pastel14b.jpg");
    textureWall = loadTexture("textures/brick005.jpg");
    textureWood = loadTexture("textures/wood001.jpg");
    textureDoor = loadTexture("textures/door067_07.jpg");
    //textureWindow = loadTexture("textures/window031_08.jpg");
    textureCeiling = loadTexture("textures/wallp016.jpg");
    textureGround = loadTexture("textures/veg011.jpg");
    textureRoof = loadTexture("textures/roof06.jpg");

    textureTop = loadTexture("textures/rocket_top.jpg");
    textureMiddle = loadTexture("textures/rocket_middle.jpg");
    textureBottom = loadTexture("textures/rocket_bottom.jpg");
    textureMotor = loadTexture("textures/rocket_motor.jpg");
}

DrawObject::~DrawObject()
{
    glDeleteTextures(1,&textureFloor);
    glDeleteTextures(1,&texture);
    glDeleteTextures(1,&textureEarth);
    //glDeleteTextures(1,&textureGlobe);
    glDeleteTextures(1,&textureWall);
    glDeleteTextures(1,&textureWood);
    glDeleteTextures(1,&textureCeiling);
    glDeleteTextures(1,&textureDoor);
    //glDeleteTextures(1,&textureWindow);
    glDeleteTextures(1,&textureGround);
    glDeleteTextures(1,&textureRoof);

    glDeleteTextures(1,&textureTop);
    glDeleteTextures(1,&textureMiddle);
    glDeleteTextures(1,&textureBottom);
    glDeleteTextures(1,&textureMotor);

}

void DrawObject::DrawRocket(double angleZ)
{

    GLUquadric* params = gluNewQuadric();
    glColor3ub(255,255,255);

    glRotated(angleZ,0,0,1);
    glBindTexture(GL_TEXTURE_2D,textureTop);
    gluQuadricTexture(params,GL_TRUE);
    gluQuadricDrawStyle(params,GLU_FILL);
    gluCylinder(params,0.5,0,1.6,20,1);

    glTranslated(0,0,-1.05);
    glBindTexture(GL_TEXTURE_2D,textureMiddle);

    gluCylinder(params,0.15,0.5,1.05,20,1);
    glTranslated(0,0,-0.25);
    glBindTexture(GL_TEXTURE_2D,textureBottom);

    gluCylinder(params,0.3,0.15,0.25,20,1);

    glBindTexture(GL_TEXTURE_2D,textureMotor);

    gluDisk(params,0,0.3,20,1);

    gluDeleteQuadric(params);
    glBindTexture(GL_TEXTURE_2D,0);


}

void DrawObject::DrawSphere()
{

    GLUquadric* params = gluNewQuadric();
    glColor3ub(255,255,255);
    gluQuadricTexture(params,GL_TRUE);
    glBindTexture(GL_TEXTURE_2D,textureEarth);


    gluSphere(params,0.34,20,20);
    gluDeleteQuadric(params);
    glBindTexture(GL_TEXTURE_2D,0);


}

void DrawObject::DrawGlobe(double angleZ)
{

    glPushMatrix();
    GLUquadric* params = gluNewQuadric();
    glColor3ub(0,0,100);
    gluDisk(params,0,0.3,20,1);
    gluCylinder(params,0.05,0.05,0.2,20,1);
    glTranslated(0,0,0.55);
    glPushMatrix();
    glRotated(45,1,0,0);
    glRotated(90,0,1,0);
    gluPartialDisk(params,0.35,0.4,20,1,0,180);
    glPopMatrix();
    glRotated(-45,1,0,0);
    glRotated(angleZ,0,0,1);
    DrawSphere();
    glPopMatrix();

}

void DrawObject::DrawCube()
{
    float l=0.5;
    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, texture);

    glBegin(GL_QUADS);

    glTexCoord2d(0,1);
    glVertex3d(l,l,l);
    glTexCoord2d(0,0);
    glVertex3d(l,l,-l);
    glTexCoord2d(1,0);
    glVertex3d(-l,l,-l);
    glTexCoord2d(1,1);
    glVertex3d(-l,l,l);

    glTexCoord2d(0,1);
    glVertex3d(l,-l,l);
    glTexCoord2d(0,0);
    glVertex3d(l,-l,-l);
    glTexCoord2d(1,0);
    glVertex3d(l,l,-l);
    glTexCoord2d(1,1);
    glVertex3d(l,l,l);

    glTexCoord2d(0,1);
    glVertex3d(-l,-l,l);
    glTexCoord2d(0,0);
    glVertex3d(-l,-l,-l);
    glTexCoord2d(1,0);
    glVertex3d(l,-l,-l);
    glTexCoord2d(1,1);
    glVertex3d(l,-l,l);

    glTexCoord2d(0,1);
    glVertex3d(-l,l,l);
    glTexCoord2d(0,0);
    glVertex3d(-l,l,-l);
    glTexCoord2d(1,0);
    glVertex3d(-l,-l,-l);
    glTexCoord2d(1,1);
    glVertex3d(-l,-l,l);

    glTexCoord2d(0,1);
    glVertex3d(l,l,-l);
    glTexCoord2d(0,0);
    glVertex3d(l,-l,-l);
    glTexCoord2d(1,0);
    glVertex3d(-l,-l,-l);
    glTexCoord2d(1,1);
    glVertex3d(-l,l,-l);

    glTexCoord2d(0,1);
    glVertex3d(l,-l,l);
    glTexCoord2d(0,0);
    glVertex3d(l,l,l);
    glTexCoord2d(1,0);
    glVertex3d(-l,l,l);
    glTexCoord2d(1,1);
    glVertex3d(-l,-l,l);

    glEnd();
    glBindTexture(GL_TEXTURE_2D,0);

}

void DrawObject::DrawHouse(float width,float length,float height,float roofHeight)
{
    DrawFloor(width,length);
    DrawWalls(width,length,height);
    DrawRoof(width,length,height,roofHeight);
    DrawCeiling(width,length,height);
}

void DrawObject::DrawLight(float height)
{
    glPushMatrix();
    glTranslated(0,0,height-1);
    GLUquadric* params = gluNewQuadric();
    glColor3ub(255,255,0);
    gluSphere(params,0.15,20,20);
    gluDeleteQuadric(params);
    glPopMatrix();
}

void DrawObject::DrawGround(float width,float length)
{

    float hsize=20;
    float hl=length/2;
    float hw=width/2;

    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureGround);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(-hsize,-hsize,-1);
    glTexCoord2i(hsize,0);
    glVertex3i(hsize,-hsize,-1);
    glTexCoord2i(hsize,(hsize-hl)/2);
    glVertex3i(hsize,-hl,-1);
    glTexCoord2i(0,(hsize-hl)/2);
    glVertex3i(-hsize,-hl,-1);

    glTexCoord2i(0,0);
    glVertex3i(-hsize,hl,-1);
    glTexCoord2i(hsize,0);
    glVertex3i(hsize,hl,-1);
    glTexCoord2i(hsize,(hsize-hl)/2);
    glVertex3i(hsize,hsize,-1);
    glTexCoord2i(0,(hsize-hl)/2);
    glVertex3i(-hsize,hsize,-1);

    glTexCoord2i(0,0);
    glVertex3i(-hsize,-hl,-1);
    glTexCoord2i((hsize-hw)/2,0);
    glVertex3i(-hw,-hl,-1);
    glTexCoord2i((hsize-hw)/2,hl);
    glVertex3i(-hw,hl,-1);
    glTexCoord2i(0,hl);
    glVertex3i(-hsize,hl,-1);

    glTexCoord2i(0,0);
    glVertex3i(hw,-hl,-1);
    glTexCoord2i((hsize-hw)/2,0);
    glVertex3i(hsize,-hl,-1);
    glTexCoord2i((hsize-hw)/2,hl);
    glVertex3i(hsize,hl,-1);
    glTexCoord2i(0,hl);
    glVertex3i(hw,hl,-1);

    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);

}


void DrawObject::DrawFloor(float width, float length)
{
    float hl=length/2;
    float hw=width/2;

    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureFloor);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(-hw,-hl,-1);
    glTexCoord2i(width,0);
    glVertex3i(hw,-hl,-1);
    glTexCoord2i(width,length);
    glVertex3i(hw,hl,-1);
    glTexCoord2i(0,length);
    glVertex3i(-hw,hl,-1);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);
}


void DrawObject::DrawCeiling(float width,float length,float height)
{
    float hl=length/2;
    float hw=width/2;
    float h=height-1;

    //glColor3ub(200,150,100);
    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureCeiling);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(-hw,-hl,h);
    glTexCoord2i(hw/2,0);
    glVertex3i(hw,-hl,h);
    glTexCoord2i(hw/2,hl/2);
    glVertex3i(hw,hl,h);
    glTexCoord2i(0,hl/2);
    glVertex3i(-hw,hl,h);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);
}

void DrawObject::DrawRoof(float width,float length,float height,float roofHeight)
{

    float hl=length/2;
    float hw=width/2;
    float h=height-1;
    float delta=0;

    glColor3ub(255,255,255);

    glBindTexture(GL_TEXTURE_2D, textureRoof);

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(hw,-hl,h);
    glTexCoord2i(length/2,0);
    glVertex3i(hw,hl,h);
    glTexCoord2i(length/2-delta/2,roofHeight);
    glVertex3i(0,hl-delta,height+roofHeight);
    glTexCoord2i(delta/2,roofHeight);
    glVertex3i(0,-hl+delta,height+roofHeight);
    glEnd();

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(-hw,hl,h);
    glTexCoord2i(length/2,0);
    glVertex3i(-hw,-hl,h);
    glTexCoord2i(length/2,roofHeight);
    glVertex3i(0,delta-hl,height+roofHeight);
    glTexCoord2i(0,roofHeight);
    glVertex3i(0,hl-delta,height+roofHeight);
    glEnd();

    glBegin(GL_TRIANGLES);
    glTexCoord2i(0,0);
    glVertex3i(-hw,hl,h);
    glTexCoord2i(width/2,0);
    glVertex3i(hw,hl,h);
    glTexCoord2d(width/4,roofHeight/2);
    glVertex3i(0,hl-delta,height+roofHeight);
    glEnd();

    glBegin(GL_TRIANGLES);
    glTexCoord2i(0,0);
    glVertex3i(-hw,-hl,h);
    glTexCoord2i(width/2,0);
    glVertex3i(hw,-hl,h);
    glTexCoord2d(width/4,roofHeight/2);
    glVertex3i(0,delta-hl,height+roofHeight);
    glEnd();

    glBindTexture(GL_TEXTURE_2D,0);

}

void DrawObject::DrawWalls(float width, float length, float height)
{

    float hl=length/2;
    float hw=width/2;
    float h=height-1;

    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureWall);
    glBegin(GL_QUADS);

    glTexCoord2i(0,0);
    glVertex3i(-hw,-hl,-1);
    glTexCoord2i(width/2,0);
    glVertex3i(hw,-hl,-1);
    glTexCoord2i(width/2,height/2);
    glVertex3i(hw,-hl,h);
    glTexCoord2i(0,height/2);
    glVertex3i(-hw,-hl,h);
    glEnd();
    /*
        glBegin(GL_QUADS);
        glTexCoord2i(0,0);
        glVertex3i(hw,hl,-1);
        glTexCoord2i(length/2,0);
        glVertex3i(hw,-hl,-1);
        glTexCoord2i(length/2,height/2);
        glVertex3i(hw,-hl,h);
        glTexCoord2i(0,height/2);
        glVertex3i(hw,hl,h);
        glEnd();
    */
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(-hw,-hl,-1);
    glTexCoord2i(length/2,0);
    glVertex3i(-hw,hl,-1);
    glTexCoord2i(length/2,height/2);
    glVertex3i(-hw,hl,h);
    glTexCoord2i(0,height/2);
    glVertex3i(-hw,-hl,h);
    glEnd();

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(hw,hl,-1);
    glTexCoord2i(width/2,0);
    glVertex3i(-hw,hl,-1);
    glTexCoord2i(width/2,height/2);
    glVertex3i(-hw,hl,h);
    glTexCoord2i(0,height/2);
    glVertex3i(hw,hl,h);

    glEnd();

    float doorW=4;
    float doorH=4;

    //porte
    glBindTexture(GL_TEXTURE_2D, textureDoor);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(5,4,-1);
    glTexCoord2i(1,0);
    glVertex3i(5,0,-1);
    glTexCoord2i(1,1);
    glVertex3i(5,0,3);
    glTexCoord2i(0,1);
    glVertex3i(5,4,3);
    glEnd();

    //murs autour
    glBindTexture(GL_TEXTURE_2D, textureWall);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(hw,hl,-1);
    glTexCoord2i((hl-doorW)/2,0);
    glVertex3i(hw,doorW,-1);
    glTexCoord2i((hl-doorW)/2,height/2);
    glVertex3i(hw,doorW,h);
    glTexCoord2i(0,height/2);
    glVertex3i(hw,hl,h);
    glEnd();

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(hw,0,-1);
    glTexCoord2i(hl/2,0);
    glVertex3i(hw,-hl,-1);
    glTexCoord2i(hl/2,height/2);
    glVertex3i(hw,-hl,h);
    glTexCoord2i(0,height/2);
    glVertex3i(hw,0,h);
    glEnd();

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3i(hw,doorW,doorH-1);
    glTexCoord2i(doorW/2,0);
    glVertex3i(hw,0,doorH-1);
    glTexCoord2i(doorW/2,(height-doorH)/2);
    glVertex3i(hw,0,h);
    glTexCoord2i(0,(height-doorH)/2);
    glVertex3i(hw,doorW,h);
    glEnd();

    /*
        glBindTexture(GL_TEXTURE_2D, textureWindow);
        glBegin(GL_QUADS);
        glTexCoord2i(0,0);
        glVertex3i(-5,-2,2);
        glTexCoord2i(1,0);
        glVertex3i(-5,2,2);
        glTexCoord2i(1,1);
        glVertex3i(-5,2,6);
        glTexCoord2i(0,1);
        glVertex3i(-5,-2,6);
        glEnd();
    */
    glBindTexture(GL_TEXTURE_2D, 0);

}

void DrawLeg(float height)
{
    glBegin(GL_QUADS);
    glTexCoord2d(0,1);
    glVertex3d(0,0,-1+height);
    glTexCoord2d(0,0);
    glVertex3d(0,0,-1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0,-1);
    glTexCoord2d(1,1);
    glVertex3d(0.1,0,-1+height);

    glTexCoord2d(0,1);
    glVertex3d(0,0,-1+height);
    glTexCoord2d(0,0);
    glVertex3d(0,0,-1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0,-1);
    glTexCoord2d(1,1);
    glVertex3d(0.1,0,-1+height);

    glTexCoord2d(0,1);
    glVertex3d(0,0,-1+height);
    glTexCoord2d(0,0);
    glVertex3d(0,0,-1);
    glTexCoord2d(1,0);
    glVertex3d(0,0.1,-1);
    glTexCoord2d(1,1);
    glVertex3d(0,0.1,-1+height);

    glTexCoord2d(0,1);
    glVertex3d(0,0.1,-1+height);
    glTexCoord2d(0,0);
    glVertex3d(0,0.1,-1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0.1,-1);
    glTexCoord2d(1,1);
    glVertex3d(0.1,0.1,-1+height);

    glTexCoord2d(0,1);
    glVertex3d(0.1,0,-1+height);
    glTexCoord2d(0,0);
    glVertex3d(0.1,0,-1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0.1,-1);
    glTexCoord2d(1,1);
    glVertex3d(0.1,0.1,-1+height);
    glEnd();
}

void DrawObject::DrawChair()
{
    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureWood);
    //siege
    glBegin(GL_QUADS);
    glTexCoord2d(0,1);
    glVertex3d(1,1,0);
    glTexCoord2d(0,0);
    glVertex3d(1,0,0);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0);
    glTexCoord2d(1,1);
    glVertex3d(0,1,0);

    glTexCoord2d(0,1);
    glVertex3d(1,1,-0.1);
    glTexCoord2d(0,0);
    glVertex3d(1,0,-0.1);
    glTexCoord2d(1,0);
    glVertex3d(0,0,-0.1);
    glTexCoord2d(1,1);
    glVertex3d(0,1,-0.1);

    glTexCoord2d(0,1);
    glVertex3d(1,1,0);
    glTexCoord2d(0,0);
    glVertex3d(1,1,-0.1);
    glTexCoord2d(1,0);
    glVertex3d(0,1,-0.1);
    glTexCoord2d(1,1);
    glVertex3d(0,1,0);

    glTexCoord2d(0,1);
    glVertex3d(1,1,0);
    glTexCoord2d(0,0);
    glVertex3d(1,1,-0.1);
    glTexCoord2d(1,0);
    glVertex3d(1,0,-0.1);
    glTexCoord2d(1,1);
    glVertex3d(1,0,0);

    glTexCoord2d(0,1);
    glVertex3d(1,0,0);
    glTexCoord2d(0,0);
    glVertex3d(1,0,-0.1);
    glTexCoord2d(1,0);
    glVertex3d(0,0,-0.1);
    glTexCoord2d(1,1);
    glVertex3d(0,0,0);

    glTexCoord2d(0,1);
    glVertex3d(0,1,0);
    glTexCoord2d(0,0);
    glVertex3d(0,1,-0.1);
    glTexCoord2d(1,0);
    glVertex3d(0,0,-0.1);
    glTexCoord2d(1,1);
    glVertex3d(0,0,0);
    glEnd();

    //dossier
    glBegin(GL_QUADS);
    glTexCoord2d(0,1);
    glVertex3d(0,1,1);
    glTexCoord2d(0,0);
    glVertex3d(0,0,1);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0);
    glTexCoord2d(1,1);
    glVertex3d(0,1,0);

    glTexCoord2d(0,1);
    glVertex3d(0.1,1,1);
    glTexCoord2d(0,0);
    glVertex3d(0.1,0,1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0,0);
    glTexCoord2d(1,1);
    glVertex3d(0.1,1,0);

    glTexCoord2d(0,1);
    glVertex3d(0,1,1);
    glTexCoord2d(0,0);
    glVertex3d(0.1,1,1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0,1);
    glTexCoord2d(1,1);
    glVertex3d(0,0,1);

    glTexCoord2d(0,1);
    glVertex3d(0,0,1);
    glTexCoord2d(0,0);
    glVertex3d(0.1,0,1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,0,0);
    glTexCoord2d(1,1);
    glVertex3d(0,0,0);

    glTexCoord2d(0,1);
    glVertex3d(0,1,1);
    glTexCoord2d(0,0);
    glVertex3d(0.1,1,1);
    glTexCoord2d(1,0);
    glVertex3d(0.1,1,0);
    glTexCoord2d(1,1);
    glVertex3d(0,1,0);

    glEnd();

    //pieds
    glPushMatrix();
    DrawLeg(0.9);
    glTranslated(0.9,0,0);
    DrawLeg(0.9);
    glTranslated(0,0.9,0);
    DrawLeg(0.9);
    glTranslated(-0.9,0,0);
    DrawLeg(0.9);
    glPopMatrix();


glBindTexture(GL_TEXTURE_2D, 0);
}

void DrawObject::DrawTable(float w,float h)
{
    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D, textureWood);

    glBegin(GL_QUADS);
    glTexCoord2d(0,1);
    glVertex3d(h,w,0.3);
    glTexCoord2d(0,0);
    glVertex3d(h,0,0.3);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0.3);
    glTexCoord2d(1,1);
    glVertex3d(0,w,0.3);

    glTexCoord2d(0,1);
    glVertex3d(h,w,0.2);
    glTexCoord2d(0,0);
    glVertex3d(h,0,0.2);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0.2);
    glTexCoord2d(1,1);
    glVertex3d(0,w,0.2);

    glTexCoord2d(0,1);
    glVertex3d(h,w,0.3);
    glTexCoord2d(0,0);
    glVertex3d(h,w,0.2);
    glTexCoord2d(1,0);
    glVertex3d(0,w,0.2);
    glTexCoord2d(1,1);
    glVertex3d(0,w,0.3);

    glTexCoord2d(0,1);
    glVertex3d(h,w,0.3);
    glTexCoord2d(0,0);
    glVertex3d(h,w,0.2);
    glTexCoord2d(1,0);
    glVertex3d(h,0,0.2);
    glTexCoord2d(1,1);
    glVertex3d(h,0,0.3);

    glTexCoord2d(0,1);
    glVertex3d(h,0,0.3);
    glTexCoord2d(0,0);
    glVertex3d(h,0,0.2);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0.2);
    glTexCoord2d(1,1);
    glVertex3d(0,0,0.3);

    glTexCoord2d(0,1);
    glVertex3d(0,w,0.3);
    glTexCoord2d(0,0);
    glVertex3d(0,w,0.2);
    glTexCoord2d(1,0);
    glVertex3d(0,0,0.2);
    glTexCoord2d(1,1);
    glVertex3d(0,0,0.3);
    glEnd();

    //pieds
    glPushMatrix();
    DrawLeg(1.2);
    glTranslated(h-0.1,0,0);
    DrawLeg(1.2);
    glTranslated(0,w-0.1,0);
    DrawLeg(1.2);
    glTranslated(-h+0.1,0,0);
    DrawLeg(1.2);
    glPopMatrix();

    glBindTexture(GL_TEXTURE_2D, 0);
}


